// 拦截器
import axios from "axios";
import memoryUtils from "./memoryUtils";
import { Loading, Message } from "element-ui";
import storageUtils from "./storageUtils";
const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 5000,
});
// 加载数据时打开和关闭动态动画
const loading = {
  loadingInstance: null,
  open: function () {
    if (this.loadingInstance == null) {
      // console.log(this.loadingInstance);
      this.loadingInstance = Loading.service({
        text: "拼命加载中",
        target: ".main",
        background: "rgba(0,0,0,.5)",
      });
      //  console.log(this.loadingInstance);
    }
  },
  close: function () {
    // console.log(this.loadingInstance);
    if (this.loadingInstance != null) {
      this.loadingInstance.close();
      console.log("结束加载");
    }
    this.loadingInstance = null;
  },
};
// 监测权限改变
const getMenus = () => {
  console.log(1);
  // console.log(memoryUtils.user.username);
  if (memoryUtils.user.role_id) {
    const roleId = memoryUtils.user.role_id;
    const userMenus = memoryUtils.user.role.menus;
    request({
      url: "/role",
      method: "post",
      data: { roleId },
    }).then((response) => {
      const res = response.data;
      if (res.status == 0) {
        const menus = res.data.menus;
        if (
          !(
            menus.length === userMenus.length && a.filter((t) => !b.includes(t))
          )
        ) {
          memoryUtils.user = {};
          storageUtils.removeUser();
          Message({ message: "当前用户权限被修改，请重新登录",
          type:"warning"
        });
        window.location.href = "/login"
      }
      }
    }).catch(err => {
      return
    });
  }
};
// 请求拦截器
request.interceptors.request.use(
  function (config) {
     var user = storageUtils.getUser();
    //  console.log(!user);
     if (!user.hasOwnProperty("username")) {
       user = JSON.parse(sessionStorage.getItem("USE_KEY"));
     }
     memoryUtils.user = user;
     if(config.url == "/updateRole"){
      getMenus()
      loading.open()
     }
     loading.open();
        
    // Do something before request is sent
    return config;
  },
  function (error) {
    loading.close();
    // Do something with request error
    return Promise.reject(error);
  }
);
// 响应拦截器
// Add a response interceptor
request.interceptors.response.use(
  function (response) {
    loading.close();
    const res = response.data;
    // console.log(res);
    if (res.status != 0) {
      Message({
        message: response.msg || "系统异常",
        type: "warning",
        duration: 3000,
      });
    }
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  },
  function (error) {
    Message({
      message: error.msg,
      type: "warning",
      duration: 3000,
    });
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  }
);
export default request;
